package com.lee.enjoy04_221113

import android.text.TextUtils
import android.util.Log
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.lee.enjoy04_221113.anno.AutoWired
import com.lee.enjoy04_221113.anno.Inject

/**
 *
 * @ProjectName: LifeCycleTest
 * @Package: com.lee.enjoy04_221113
 * @ClassName: InjtectUtils
 * @Description: java类作用描述
 * @Author: 李占伟
 * @CreateDate: 2022/11/13 15:04
 * @UpdateUser: 更新者
 * @UpdateDate: 2022/11/13 15:04
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */

object InjtectUtils {
    const val TAG = "InjectUtils"
    fun inject(activity:AppCompatActivity) {
        var javaClass = activity.javaClass
        var declaredFields = javaClass.declaredFields

        declaredFields.forEach {
            it.isAccessible = true
            Log.i(TAG,"属性="+it.name)
            var annotationPresent = it.isAnnotationPresent(AutoWired::class.java)
            if (annotationPresent){

                var annotation = it.getAnnotation(AutoWired::class.java)
                var annovalue = annotation.value
                Log.i(TAG,"AutoWired注解存在==$annovalue")
                if (annovalue.isBlank()){
                    annovalue = it.name
                }
                var bundleExtra = activity.intent.getBundleExtra("bundle")
                val bb = bundleExtra?.get(annovalue)
                Log.i(TAG,"bb=$bb")
                it.set(activity,bb);
            }else{
                Log.i(TAG,"${it.name},AutoWired注解不存在")
            }


        }
    }

    fun injectView(activity:AppCompatActivity) {
        var javaClass = activity.javaClass
        var declaredFields = javaClass.declaredFields

        declaredFields.forEach {
            var annotationPresent = it.isAnnotationPresent(Inject::class.java)
            if (annotationPresent){
                var annotation = it.getAnnotation(Inject::class.java)
                var annovalue = annotation.value
                Log.i(TAG,"AutoWired注解存在==$annovalue")
                var bb = activity.findViewById(annovalue) as View
                it.isAccessible = true
                it.set(activity,bb);
            }
        }
    }
}